/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.action.equiposRed;

import com.mapping.DetallesRed;
import com.mapping.Dispositivo;
import com.mapping.Red;
import com.opensymphony.xwork2.ActionSupport;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;

/**
 *
 * @author Ricardo
 */
@Namespace(value = "/")
@Action(value = "altaEquipoRed", results = {
    @Result(name = "success", location = "/GestionEquiposRed/resultadoEquiposRed.jsp")})
public class AgregarEquipoRed extends ActionSupport{
    private DetallesRed detalle;
    private String ip;
    private String mac;
    private String idDispositivo;
    private String idRed;
    private String mensaje = "Se dado de alta el Equipo en la Red :";
    
    @Override
    public String execute() {      
        detalle = new DetallesRed();
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("inventarios");
        EntityManager em = emf.createEntityManager();
        EntityTransaction tx = em.getTransaction();
        tx.begin();
        detalle.setIp(ip);
        detalle.setMac(mac);
        detalle.setIdDisp((Dispositivo) em.getReference(Dispositivo.class, idDispositivo));
        detalle.setIdRed((Red) em.getReference(Red.class, idRed));        
        em.persist(detalle);
        em.flush();
        tx.commit();
        em.close();
        return SUCCESS;
    }    

    public DetallesRed getDetalle() {
        return detalle;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public void setMac(String mac) {
        this.mac = mac;
    }

    public void setIdDispositivo(String idDispositivo) {
        this.idDispositivo = idDispositivo;
    }

    public void setIdRed(String idRed) {
        this.idRed = idRed;
    }

    public String getMensaje() {
        return mensaje;
    }
        
}
